using System.Collections.Generic;

namespace DBUtil.Provider.SqlServer;

/// <summary>
/// DBAccess的扩展方法
/// </summary>
public static class DBAccessExtensions
{
    #region IsSqlServer
    /// <summary>
    /// 是否是SqlServer数据库
    /// </summary>
    /// <param name="db"></param>
    /// <returns></returns>
    public static bool IsSqlServer(this DBAccess db)
    {
        return db.DBType == EnumDBType.SqlServer;
    }
    #endregion

    #region SqlServer版本兼容性判断
    /* 其实建立数据库时可以指定db的兼容级别, 但认为一般不会用高版本的数据库见一个低版本的db
     * 查询sql: SELECT compatibility_level FROM sys.databases WHERE name = DB_NAME();
     * 列举的每个版本的兼容级别
     * SQL Server 版本	默认兼容级别	可设置的最低兼容级别
     * SQL Server 2022	160	100
     * SQL Server 2019	150	100
     * SQL Server 2017	140	100
     * SQL Server 2016	130	100
     * SQL Server 2014	120	100
     * SQL Server 2012	110	100
     * SQL Server 2008 R2	100	80、90
     * SQL Server 2008	100	80、90
     * SQL Server 2005	90	80
     * SQL Server 2000	80	80
     */

    /* 2008
    Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)   
    Apr  2 2010 15:48:46   
    Copyright (c) Microsoft Corporation  
    Enterprise Edition (64-bit) on Windows NT 6.1 <X64> (Build 7601: Service Pack 1) 
    */
    /* 2012
    Microsoft SQL Server 2012 (SP1) - 11.0.3000.0 (X64) 
	        Oct 19 2012 13:38:57 
	        Copyright (c) Microsoft Corporation
	        Developer Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
    */
    /* 2014
      Microsoft SQL Server 2014 (SP2) (KB3171021) - 12.0.5000.0 (X64) 
      Jun 17 2016 19:14:09 
      Copyright (c) Microsoft Corporation
      Enterprise Edition (64-bit) on Windows NT 6.3 <X64> (Build 17763: )
    */
    /* 2019
     Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64) 
        Sep 24 2019 13:48:23 
        Copyright (C) 2019 Microsoft Corporation
        Developer Edition (64-bit) on Windows 10 Pro 10.0 <X64> (Build 19045: )   
     */
    /* 2022
     Microsoft SQL Server 2022 (RTM) - 16.0.1000.6 (X64)   
        Oct  8 2022 05:58:25
        Copyright (C) 2022 Microsoft Corporation
        Enterprise Evaluation Edition (64-bit) on Windows 10 Enterprise Evaluation 10.0 <X64> (Build 22621: ) (Hypervisor) 
     */
    /*
      由上面版本的字符串,推测出其他版本的字符串如下:
      Microsoft SQL Server 2008
      Microsoft SQL Server 2008 R2
      Microsoft SQL Server 2012
      Microsoft SQL Server 2014
      Microsoft SQL Server 2016
      Microsoft SQL Server 2017
      Microsoft SQL Server 2019
      Microsoft SQL Server 2022
      这里只考虑2008R2及以上的
     */
    private static readonly List<(int index, string versionstr)> Versions =
    [
        (0,"Microsoft SQL Server 2008 R2"),
        (1,"Microsoft SQL Server 2012"),
        (2,"Microsoft SQL Server 2014"),
        (3,"Microsoft SQL Server 2016"),
        (4,"Microsoft SQL Server 2017"),
        (5,"Microsoft SQL Server 2019"),
        (6,"Microsoft SQL Server 2022")
    ];
    private static bool JudgeVersion(DBAccess db, string version, int level)
    {
        int index = Versions.FindIndex(ver => version.Contains(ver.versionstr));
        switch (index)
        {
            case 0:
                {
                    db.Setting.SetDBVersionCompatibility(EnumDBVersionCompatibility.SqlServer2008Compatible);
                    break;
                }
            case 1:
                {
                    db.Setting.SetDBVersionCompatibility(EnumDBVersionCompatibility.SqlServer2012Compatible);
                    break;
                }
            case 2:
                {
                    db.Setting.SetDBVersionCompatibility(EnumDBVersionCompatibility.SqlServer2014Compatible);
                    break;
                }
            case 3:
                {
                    db.Setting.SetDBVersionCompatibility(EnumDBVersionCompatibility.SqlServer2016Compatible);
                    break;
                }
            case 4:
                {
                    db.Setting.SetDBVersionCompatibility(EnumDBVersionCompatibility.SqlServer2017Compatible);
                    break;
                }
            case 5:
                {
                    db.Setting.SetDBVersionCompatibility(EnumDBVersionCompatibility.SqlServer2019Compatible);
                    break;
                }
            case 6:
                {
                    db.Setting.SetDBVersionCompatibility(EnumDBVersionCompatibility.SqlServer2022Compatible);
                    break;
                }
            default:
                {
                    break;
                }
        }
        return index >= level;
    }

    /// <summary>
    /// 是否兼容SqlServer2008 R2
    /// </summary>
    /// <returns></returns>
    public static bool IsSqlServerVersion2008Compatible(this DBAccess db)
    {
        if (db.Setting.DBVersionCompatibility != null)
            return db.DBType == EnumDBType.SqlServer && db.Setting.DBVersionCompatibility >= EnumDBVersionCompatibility.SqlServer2008Compatible;
        return JudgeVersion(db, db.DBVersion, 0);
    }

    /// <summary>
    /// 是否兼容SqlServer2012
    /// </summary>
    /// <returns></returns>
    public static bool IsSqlServerVersion2012Compatible(this DBAccess db)
    {
        if (db.Setting.DBVersionCompatibility != null)
            return db.DBType == EnumDBType.SqlServer && db.Setting.DBVersionCompatibility >= EnumDBVersionCompatibility.SqlServer2012Compatible;
        return JudgeVersion(db, db.DBVersion, 1);
    }

    /// <summary>
    /// 是否兼容SqlServer2014
    /// </summary>
    /// <returns></returns>
    public static bool IsSqlServerVersion2014Compatible(this DBAccess db)
    {
        if (db.Setting.DBVersionCompatibility != null)
            return db.DBType == EnumDBType.SqlServer && db.Setting.DBVersionCompatibility >= EnumDBVersionCompatibility.SqlServer2014Compatible;
        return JudgeVersion(db, db.DBVersion, 2);
    }

    /// <summary>
    /// 是否兼容SqlServer2016
    /// </summary>
    /// <returns></returns>
    public static bool IsSqlServerVersion2016Compatible(this DBAccess db)
    {
        if (db.Setting.DBVersionCompatibility != null)
            return db.DBType == EnumDBType.SqlServer && db.Setting.DBVersionCompatibility >= EnumDBVersionCompatibility.SqlServer2016Compatible;
        return JudgeVersion(db, db.DBVersion, 3);
    }
    /// <summary>
    /// 是否兼容SqlServer2017
    /// </summary>
    /// <returns></returns>
    public static bool IsSqlServerVersion2017Compatible(this DBAccess db)
    {
        if (db.Setting.DBVersionCompatibility != null)
            return db.DBType == EnumDBType.SqlServer && db.Setting.DBVersionCompatibility >= EnumDBVersionCompatibility.SqlServer2017Compatible;
        return JudgeVersion(db, db.DBVersion, 4);
    }
    /// <summary>
    /// 是否兼容SqlServer2019
    /// </summary>
    /// <returns></returns>
    public static bool IsSqlServerVersion2019Compatible(this DBAccess db)
    {
        if (db.Setting.DBVersionCompatibility != null)
            return db.DBType == EnumDBType.SqlServer && db.Setting.DBVersionCompatibility >= EnumDBVersionCompatibility.SqlServer2019Compatible;
        return JudgeVersion(db, db.DBVersion, 5);
    }
    /// <summary>
    /// 是否兼容SqlServer2019
    /// </summary>
    /// <returns></returns>
    public static bool IsSqlServerVersion2022Compatible(this DBAccess db)
    {
        if (db.Setting.DBVersionCompatibility != null)
            return db.DBType == EnumDBType.SqlServer && db.Setting.DBVersionCompatibility >= EnumDBVersionCompatibility.SqlServer2022Compatible;
        return JudgeVersion(db, db.DBVersion, 6);
    }
    #endregion
}
